﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ankha.Core;

namespace Errors
{
    public class DefinedError
    {
        public int ErrorCode { get; private set; }
        public string ErrorMessage { get; private set; }
        public object[] Data { get; private set; }
        public bool Fatal { get; private set; }

        public DefinedError(int code, string msg, params object[] data)
            : this(code, msg, true, data)
        {

        }

        public DefinedError(int code, string msg, bool Fatal, params object[] data)
        {
            this.ErrorCode = code;
            this.ErrorMessage = msg;
            this.Data = data;
            this.Fatal = Fatal;
        }

        public string Message
        {
            get
            {
                string errormsg = ErrorMessage;
                if (Data != null)
                    errormsg = string.Format(ErrorMessage, Data);

                return string.Format("IvoryFrameWorkException #{0,3}: {1}", ErrorCode.ToString().PadLeft(3, '0'), errormsg);
            }
        }
    }

    public class FrameworkError : Exception
    {
        public FrameworkError(DefinedError error)
            : base(error.Message)
        {

        }
    }

    public static partial class Error
    {
        public static DefinedError AssetLoadingException { get { return new DefinedError(1, "Manager Failed to Load Assets", null); } }
        public static DefinedError NoMainMenuException { get { return new DefinedError(4, "No Main Menu has been set", null); } }
        public static DefinedError InvalidUseException { get { return new DefinedError(6, "GetCurrentScreen() has Returned a Splashscreen :/", null); } }
        public static DefinedError InvalidLoadingScreenException { get { return new DefinedError(5, "{0} has no Loading Screen, all Gamescreens require a Loadingscreen", null); } }
        
        public static DefinedError FlagRecognisedException(string flag)
        {
            return new DefinedError(7, "Flag {0} already recognised by the Configuration", flag);
        }
        public static DefinedError ConfigLoadException(string file)
        {
            return new DefinedError(8, "Error Reading the Config File {0}", file);
        }
        public static DefinedError ConfigReadException(string file, int line)
        {
            return new DefinedError(8, "Error Reading the Config File {0} @line={1}", file, line);
        }
        public static DefinedError ConfigRequestException(string flag)
        {
            return new DefinedError(9, "Error Requesting Flag {0}", flag);
        }

        public static DefinedError InvalidBindingException(Binding binding)
        {
            return new DefinedError(13, "The property '{0}' was not found on object of type '{1}'", binding.BindingProperty, binding.BindingObj.GetType());
        }
    }
}
